<?php if (isset($success_message)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">Guia creada con exito!</span></div>
    </div>
<?php endif; ?>
<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_formAddPurchase" style="cursor: pointer; left: 559.5px;">
    <div class="noty_message"><span id="contentMessageErrorFormAddPurchase" class="noty_text">Debe agregar algun producto</span></div>
</div>


<script type="text/javascript">
    $(document).ready(function() {
		
		

<?php if (isset($searchsales)) {
    
} else { ?>

//            $('li[id=type_document_chzn_o_2]').click(function() {
//                $.ajax({
//                    'url': 'index.php/sales/codeguia',
//                    'success': function(res) {
//                        $('input[name=numero]').val('');
//                        $('input[name=numero]').val(res);
//                        $('#numero').css('background', '#ffffff');
//                    }
//                });
//
//            })
//            
//            $('li[id=type_document_chzn_o_1]').click(function() {
//                $.ajax({
//                    'url': 'index.php/sales/codeguia',
//                    'success': function(res) {
//                        alert('boleta');
//                        $('#numero').css('background', '#ffffff');
//                    }
//                });
//
//            })
//            
//            $('li[id=type_document_chzn_o_0]').click(function() {
//                $.ajax({
//                    'url': 'index.php/sales/codeguia',
//                    'success': function(res) {
//                        alert('factura');
//                        $('#numero').css('background', '#ffffff');
//                    }
//                });
//
//            })
            
            $('li[id=type_document_chzn_o_1]').click(function() {
                $.ajax({
                    'url': '<?php echo base_url(); ?>index.php/sales_shop/codeguia',
                    'success': function(res) {
                       alert('hola');
                        $('input[name=numero]').val('');
                        $('input[name=numero]').val(res);
                        $('#numero').css('background', '#ffffff');
                        
                    }
                });
            })

<?php } ?>

        $('#noty_success_1355937644582').click(function() {
            $('#noty_success_1355937644582').fadeOut('fast');
        });

        
       

    })
</script>
<div class="grid_container">
    <form id="formAddPurchase" action="<?php if (isset($searchsales)) {
    echo 'search';
} else {
    echo 'submit';
} ?> " method="post" class="form_container">
        <div class="grid_12">
            <div class="widget_wrap">
                <div class="widget_top">
                    <span class="h_icon magnifying_glass"></span>
                    <h6>
<?php if (isset($searchsales)) {
    echo 'Buscar Guía de Compra'; ?>
                            <a class="action-icons c-add" href="<?php echo base_url(); ?>index.php/sales_shop/add" original-title="Nuevo">Nuevo</a>
<?php } else {
    echo 'Nueva Guía de Compra'; ?>
                            <a class="action-icons c-view" href="<?php echo base_url(); ?>index.php/sales_shop/" original-title="Buscar">Buscar</a>
<?php } ?>
                    </h6>
                </div>

                <div class="widget_content" style="margin-bottom: 30px">
                    <ul>
                        <li>
                            <div class="form_grid_12" >
                                <div class="form_input">
                                    <div class=" form_grid_4 alpha" style=" width: 290px;">
                                        <span class="label_head" for="type_document">Tipo de Documento</span>
<?php
$options = array();
foreach ($typeDocuments as $td) {
    $options[$td->id] = $td->description;
}
$extra = 'id="type_document" tabindex="1" style="width:250px; height:25px" class="chzn-select full"';
echo form_dropdown('tipo', $options, '3', $extra);
?>
                                    </div>
                                    <div class=" form_grid_4 alpha" style=" width: 290px;">
                                        <span class="label_head">N° Documento</span>
                                        <input type="text" name="numero" id="numero" class="noEmpty" value="<?php if (isset($datesale)) {
                                        foreach ($datesale as $da) {
                                            echo $da->document_code;
                                        }
                                    } ?>" tabindex="2">

                                    </div>
                                    <div class=" form_grid_2">
                                        <span class="label_head">Fecha</span>
                                        <input type="text" name="date" id="numero" value="<?php if (isset($datesale)) {
                                        foreach ($datesale as $da) {
                                            echo $da->date;
                                        }
                                    } else {
                                        echo date('d/m/Y');
                                    } ?>" tabindex="3">
                                    </div>
                                    
                                    
                                    <div class=" form_grid_2" style=" width: 100px;">
                                        <span class="label_head">%Igv</span>
                                        <input type="text" name="igv" id="numero" value="18" tabindex="3">
                                    </div>
                                    
                                    
                                    <div class=" form_grid_2" style=" width: 100px;">
                                        <span class="label_head">T.Cambio</span>
                                        <input type="text" name="t_change" id="numero" value="0.00" tabindex="3" placeholder="0.00">
                                    </div>
                                    
                                    
                                    <fieldset style="width: 160px; float: left; margin-top: 5px;">
                                         <legend>Moneda:</legend>
                                         <label><input type="radio" value="d" name="money" style="margin-top: 6px; margin-bottom: 13px;">:Dolares</label>&nbsp;&nbsp;&nbsp;
                                         <label><input type="radio" value="s" name="money" checked="true">:Soles</label>
                                     </fieldset>
                                    
                                    
                                    <fieldset style="width: 30%; float: left; margin-left: 30px; margin-top: 5px;">
                                         <legend>Datos para Calculo de Costo:</legend>
                                         %Utilidad:&nbsp;<input type="text" value="0.00" name="utilidad" style="width:60px;" placeholder="0.00">&nbsp;&nbsp;&nbsp;
                                         Otros Costos:&nbsp;<input type="text" value="0.00" name="costo" style="width:60px;" placeholder="0.00">
                                     </fieldset>
                                    
                                     
                                    <span class="clear"></span>
                                </div>
                            </div>
                      
                            <div class="form_grid_12">
                                <div class="form_input">
                                    <div class="form_grid_12 alpha">
                                        <span class="label_head">Observaciones</span>
                                        <input type="text" name="observations" id="observations" tabindex="5" value="<?php if (isset($datesale)) {
    foreach ($datesale as $da) {
        echo $da->observations;
    }
} else {
    
} ?>">
                                    </div>
                                    <span class="clear"></span>
                                </div>
                            </div>
                        </li>
                    </ul>
<?php if (isset($searchsales)) { ?>
                    
                    
                     <div class="form_grid_2" style="margin-bottom: 10px; float: right;">
                            <button type="button" id="buscar_sale_shop"  class="btn_small btn_blue"><span>Buscar</span></button>
                        </div>
                            <?php } else { ?>
                    
                    <script>
                    $(document).ready(function(){
                        $('#add_products').click(function(e) {
                                $.ajax({
                                    'url' : url+'/sales_shop/products/',
                                    'type' : 'POST',
                                    'data' : {
                                        'code' : '',
                                        'description' : ''
                                    },
                                    'success' : function(res){
                                        $('#add-product-model').html(res);
                                        }
                                    });
                                        $('#add-product-model').modal();
                                        return false;
                                });
	
                    })
                    </script>
                            
                    
                        <div class="form_grid_2 " style="margin-bottom: 10px; float: right">
                            <button id="add_products"  class="btn_small btn_blue"><span>Agregar Producto</span></button>
                        </div>

<?php } ?>

                    
                    <table class="wtbl_list">
                        <thead>
                            <tr class="tr_1">
                                <th width="10%" class="hola">Código</th>
                                <th width="30%">Descripción</th>
                                <th width="10%">Cajas</th>
                                <th width="10%">Empaques</th>
                                <th width="10%">Unidades</th>
                                <th width="20%">Flete</th>
                                <th width="20%">Seguro</th>
                                <th width="10%">Ad.Val</th>
                                <th width="10%">Val.Unit</th>
                                <th width="10%">Total</th>
                                <th width="10%"></th>
                            </tr>

                        </thead>

                        <tbody id="contentProductList" class="contentProductList">
                            


<input type="text" name="contador_detalles" id="contador_detalles" value="0" >
                        </tbody>



                    </table>
                   

                    <input type="text" name="subtotalgeneral" id="subtotalgeneral" value="0.00" placeholder="0.00" readonly="true" style=" text-align:center; width: 100px; margin-right: 58px; float: right; background: #D0D0D0; font-family: arial; font-size: 15px; font-weight: bold;">
                    <input type="hidden" name="contenaumentador" id="contenaumentador" value="0">
                    




                </div>
            </div>
            <br />
<?php if (!isset($parametro) == 1) : ?>
                <script type="text/javascript">
                    $(document).ready(function() {

                        function checkCode() {

                            $.ajax({
                                'url': url + '/Sales_shop/search_code_unique',
                                'type': 'POST',
                                'data': {'numero': $('input[name=numero]').val()},
                                'success': function(res) {
                                    //alert("alert res: " + res);
                                        if (res == 1) {

                                       

                                        alert('El codigo del documento ya fue ingresado anteriormente Sistema!!');
                                        $('#numero').css('background', '#ff3333');
                                        

                                    } else if (res == 0) {

                                       
                                        $('#numero').css('background', '#ffffff');
                                        $('#formAddPurchase').submit();
                                    }
                                }
                            });

                        }

                        $('#numero').focus(function() {
                            $('#numero').css('background', '#ffffff');
                        })
                        $('input[name=date]').focus(function() {
                            $('input[name=date]').css('background', '#ffffff');
                        })


                        function formtrue() {
                            $('formAddPurchase').submit(function() {
                                return true;
                            });
                        }
                        function formfalse() {
                            $('formAddPurchase').submit(function() {
                                return false;
                            });
                        }

                        $('#guardar_guia').click(function() {
                            if ($('#contador_detalles').val() >= 1 && $('#numero').val()!='') {

                                alert('existe');
                                $('div[id=noty_success_1355937644582]').css('display', 'block');
                                $('div[id=message_error_formAddPurchase]').css('display', 'none');

                                checkCode();

                            } else {
                                alert('No existe');
                                $('div[id=noty_success_1355937644582]').css('display', 'none');
                                $('div[id=message_error_formAddPurchase]').css('display', 'block');

                                if ($('#numero').val() == '') {
                                    $('#numero').css('background', '#ff3333');
                                }

                                var fechaactual = new Date();


                                if ($('input[name=date]').val() == '') {
                                    $('input[name=date]').css('background', '#ff3333');
                                }





                            }

                        });
                    })
                </script>

                <button type="button" id="guardar_guia" class="btn_small btn_orange"><span>Guardar</span></button>
                            <?php endif; ?>
        </div>
    </form>
    <span class="clear"></span>

    <div id="add-product-model">
    </div>
</div>

<table id="listProductBlank" class="clear">
    <tr id="rowListProductBlank" class="tr_even">
        <td class="elementList codeProduct"></td>
        <td class="elementList descriptionProduct"></td>
        <td class="elementList quantityBoxesProduct"></td>
        <td class="elementList quantityPackagesProduct"></td>
        <td class="elementList quantityUnitsProduct"></td>
        <td class="elementList priceProduct"></td>
        <td class="elementList inputProduct"><a href="#" class="action-icons c-Delete delProductList" original-title="Borrar">Delete</a></td>
    </tr>
</table>